/*	kd:
	
	Here I implement some convenience for spawning particles in the shop.
	
	*/

struct Gc_Shop_Effects {
	
	// kd: The particle_fx isn't private on purpose (as are many other fields
	// with me saying "rules are supposed to help you").
	private vector2						cursor_pos;
	Gc_ScreenParticles					particle_fx;
	
	// kd: The particles on the ring buttons:
	Gc_Shop_ItemFx						item_fx;
	private bool						can_item_fx_move;
	
	// kd: Either effects:
	private bool						is_shown;
	private Gc_DefaultCvar				cvar_fx;
	
	void OnOpen () {
		cvar_fx.Find("gc_shop_fx");
		is_shown = cvar_fx.GetBool(def_val: true);
	}
	
	void Save () {
		cvar_fx.SetBool(is_shown);
	}
	
	void Toggle () {
		is_shown = !is_shown;
	}
	
	bool IsShown () const {
		return is_shown;
	}
	
	void LoadShopFx (Gc_Shop_Inv current_shop) {
		item_fx.OnInventoryLoad(current_shop);
		can_item_fx_move = false;
	}
	
	void BeginShopFx (
	Gc_Shop_InvLayout	ring_layout,
	vector2				window_resolution) {
		item_fx.StickToLayout(ring_layout, window_resolution);
		item_fx.Trigger();
		can_item_fx_move = true;
	}
	
	// kd: Update stuff you attached in the shop:
	void NextItemFxState (
	Gc_Shop_InvLayout	ring_layout,
	vector2				window_resolution) {
		if(!can_item_fx_move || !is_shown) {
			return;
		}
		
		item_fx.StickToLayout(ring_layout, window_resolution);
		item_fx.NextState();
	}
	
	// kd: Update stuff shot by your cursor:
	void NextExtraFxState () {
		if(!is_shown) {
			return;
		}
		
		particle_fx.RemoveNulls();
		particle_fx.NextState();
	}
	
	void SpawnPosTo (vector2 pos) {
		cursor_pos = pos;
	}
	
	void BuyFx (Gc_Shop_Inv inv, int item_index) {
		if(inv == NULL || !is_shown) {
			return;
		}
		
		let price = inv.Price(item_index);
		Gc_ScreenParticleGen spark_fx;
		
		if(price < 2000) {
			MakeSparks("spspi0", "spspj0", cursor_pos, 1.0);
		}
		
		else if(price < 5000) {
			MakeSparks("spspk0", "spspl0", cursor_pos, 1.5);
		}
		
		else if(price < 10000) {
			MakeSparks("spspm0", "spspn0", cursor_pos, 1.75, 1.2);
			MakeSparks("spspk0", "spspl0", cursor_pos, 1.75, 1.0);
			MakeFx("Gc_Spart_Confetti", cursor_pos);
		}
		
		else {
			MakeSparks("spspi0", "spspj0", cursor_pos + (-200, -200), 0.6);
			MakeSparks("spspi0", "spspj0", cursor_pos + (-200,  200), 0.6);
			MakeSparks("spspi0", "spspj0", cursor_pos + ( 200, -200), 0.6);
			MakeSparks("spspi0", "spspj0", cursor_pos + ( 200,  200), 0.6);
			MakeSparks("spspk0", "spspl0", cursor_pos + ( 100,    0), 0.9);
			MakeSparks("spspk0", "spspl0", cursor_pos + (   0,  100), 0.9);
			MakeSparks("spspk0", "spspl0", cursor_pos + (-100,    0), 0.9);
			MakeSparks("spspk0", "spspl0", cursor_pos + (   0, -100), 0.9);
			MakeSparks("spspm0", "spspn0", cursor_pos, 3.75, 1.2);
			MakeFx("Gc_Spart_Confetti", cursor_pos);
			MakeFx("Gc_Spart_Confetti", cursor_pos);
		}
	}
	
	private void MakeSparks (
	String	spark_name,
	String	glow_name,
	vector2	pos		= (0, 0),
	double	spawn_f = 1,
	double	scale_f = 1) {
		let gen_inst = Gc_Spart_Sparks(new("Gc_Spart_Sparks"));
		
		if(gen_inst == NULL) {
			return;
		}
		
		gen_inst.SetOrigin(pos);
		gen_inst.CustomInit(spark_name, glow_name, spawn_f, scale_f);
		particle_fx.Add(gen_inst);
		gen_inst.Trigger();
	}
	
	private void MakeFx (
	class <Gc_ScreenParticleGen> gen,
	vector2 pos = (0, 0)) {
		let gen_inst = Gc_ScreenParticleGen(new(gen));
		
		if(gen_inst == NULL) {
			return;
		}
		
		gen_inst.SetOrigin(pos);
		particle_fx.Add(gen_inst);
		gen_inst.Trigger();
	}
	
	ui void Draw (vector2 resolution, double t = 1) const {
		if(is_shown) {
			particle_fx.Draw(resolution, t);
			item_fx.Draw(resolution, t);
		}
	}
}